home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / cw_fslider.pro < prev    next >
Text File  |  1997-07-08  |  9KB  |  273 lines

  1. ; $Id: cw_fslider.pro,v 1.9 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1992-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;    CW_FSLIDER
  8. ;
  9. ; PURPOSE:
  10. ;    The standard slider provided by the WIDGET_SLIDER() function is
  11. ;    integer only. This compound widget provides a floating point
  12. ;    slider.
  13. ;
  14. ; CATEGORY:
  15. ;    Compound widgets.
  16. ;
  17. ; CALLING SEQUENCE:
  18. ;    widget = CW_FSLIDER(Parent)
  19. ;
  20. ; INPUTS:
  21. ;       Parent:        The ID of the parent widget.
  22. ;
  23. ; KEYWORD PARAMETERS:
  24. ;    DRAG:        Set this keyword to zero if events should only
  25. ;            be generated when the mouse is released. If it is
  26. ;            non-zero, events will be generated continuously
  27. ;            when the slider is adjusted. Note: On slow systems,
  28. ;            /DRAG performance can be inadequate. The default
  29. ;            is DRAG=0.
  30. ;       EDIT:        Set this keyword to make the slider label be
  31. ;            editable. The default is EDIT=0.
  32. ;    FORMAT:        Provides the format in which the slider value is
  33. ;            displayed. This should be a format as accepted by
  34. ;            the STRING procedure. The default is FORMAT='(G13.6)'
  35. ;    FRAME:        Set this keyword to have a frame drawn around the
  36. ;            widget. The default is FRAME=0.
  37. ;    MAXIMUM:    The maximum value of the slider. The default is 
  38. ;            MAXIMUM=100.
  39. ;    MINIMUM:    The minimum value of the slider. The default is
  40. ;            MINIMUM=0.
  41. ;    SCROLL        Sets the SCROLL keyword to the WIDGET_SLIDER underlying
  42. ;            this compound widget. Unlike WIDGET_SLIDER, the
  43. ;            value given to SCROLL is taken in the floating units
  44. ;            established by MAXIMUM and MINIMUM, and not in pixels.
  45. ;    SUPPRESS_VALUE:    If true, the current slider value is not displayed.
  46. ;            The default is SUPPRESS_VALUE=0.
  47. ;    TITLE:        The title of slider. (The default is no title.)
  48. ;    UVALUE:        The user value for the widget.
  49. ;    VALUE:        The initial value of the slider
  50. ;    VERTICAL:    If set, the slider will be oriented vertically.
  51. ;            The default is horizontal.
  52. ;    XSIZE:        For horizontal sliders, sets the length.
  53. ;    YSIZE:        For vertical sliders, sets the height.
  54. ;
  55. ; OUTPUTS:
  56. ;       The ID of the created widget is returned.
  57. ;
  58. ; SIDE EFFECTS:
  59. ;    This widget generates event structures containing a field
  60. ;    named value when its selection thumb is moved. This is a
  61. ;    floating point value.
  62. ;
  63. ; PROCEDURE:
  64. ;    WIDGET_CONTROL, id, SET_VALUE=value can be used to change the
  65. ;        current value displayed by the widget.
  66. ;
  67. ;    WIDGET_CONTROL, id, GET_VALUE=var can be used to obtain the current
  68. ;        value displayed by the widget.
  69. ;
  70. ; MODIFICATION HISTORY:
  71. ;    April 2, 1992, SMR and AB
  72. ;        Based on the RGB code from XPALETTE.PRO, but extended to
  73. ;        support color systems other than RGB.
  74. ;    5 January 1993, Mark Rivers, Brookhaven National Labs
  75. ;        Added EDIT keyword. 
  76. ;       7 April 1993, AB, Removed state caching.
  77. ;    28 July 1993, ACY, set_value: check labelid before setting text.
  78. ;    3 October 1995, AB, Added SCROLL keyword.
  79. ;-
  80.  
  81.  
  82. PRO fslider_set_value, id, value
  83.  
  84.   ; Set the value of both the slider and the label
  85.   ON_ERROR, 2                        ;return to caller
  86.  
  87.   stash = WIDGET_INFO(id, /CHILD)
  88.   WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
  89.  
  90.   WIDGET_CONTROL, state.slideid, $
  91.     SET_VALUE = 1000000. * $
  92.         (float(value) - state.bot) / (state.top - state.bot)
  93.   IF (state.labelid NE 0) THEN $
  94.       WIDGET_CONTROL, state.labelid, $
  95.         SET_VALUE = STRING(FLOAT(value), format=state.format)
  96.  
  97.   WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
  98. END
  99.  
  100.  
  101.  
  102. FUNCTION fslider_get_value, id
  103.  
  104.   ; Return the value of the slider
  105.   ON_ERROR, 2                        ;return to caller
  106.  
  107.   stash = WIDGET_INFO(id, /CHILD)
  108.   WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
  109.  
  110.   WIDGET_CONTROL, state.slideid, GET_VALUE = tmp
  111.   ret = ((tmp / 1000000.) * (state.top - state.bot)) + state.bot
  112.  
  113.   WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY 
  114.   return, ret
  115. END
  116.  
  117.  
  118. ;-----------------------------------------------------------------------------
  119.  
  120. FUNCTION fslide_event, ev
  121.  
  122.   ; Retrieve the structure from the child that contains the sub ids
  123.   parent=ev.handler
  124.   stash = WIDGET_INFO(parent, /CHILD)
  125.   WIDGET_CONTROL, stash, GET_UVALUE=state, /NO_COPY
  126.  
  127.  
  128.   ; See which widget was adjusted, the slider or the label
  129.  
  130.   if (ev.id eq state.slideid) then begin
  131.     ; Get the non-adjusted value
  132.     WIDGET_CONTROL, state.slideid, GET_VALUE = nonadj
  133.     ; Compute the floating point value
  134.     value = ((nonadj / 1000000.) * (state.top - state.bot)) + state.bot
  135.     drag = ev.drag
  136.     ; Update label
  137.     IF (state.labelid NE 0) THEN $
  138.       WIDGET_CONTROL, state.labelid, $
  139.            SET_VALUE=STRING(value, format=state.format)
  140.  
  141.   endif else if (ev.id eq state.labelid) then begin
  142.  
  143.     WIDGET_CONTROL, state.labelid, GET_VALUE = tmp
  144.  
  145.     value = float(tmp[0])
  146.     value = value > state.bot
  147.     value = value < state.top
  148.     ;Update the slider, set new value
  149.     WIDGET_CONTROL, state.slideid, $
  150.     SET_VALUE = 1000000. * $
  151.         (value - state.bot) / (state.top - state.bot)
  152.  
  153.     drag = 0
  154.     ; Update the label so it has desired format
  155.     WIDGET_CONTROL, state.labelid, $
  156.            SET_VALUE=STRING(value, format=state.format)
  157.   endif
  158.  
  159.   WIDGET_CONTROL, stash, SET_UVALUE=state, /NO_COPY
  160.   RETURN, { ID:parent, TOP:ev.top, HANDLER:0L, VALUE:value, DRAG:drag }
  161. END
  162.  
  163. ;-----------------------------------------------------------------------------
  164.  
  165. FUNCTION cw_fslider, parent, $
  166.         DRAG = drag, $
  167.                 EDIT = edit, $
  168.         FRAME = frame, $
  169.         MAXIMUM = max, $
  170.         MINIMUM = min, $
  171.         SCROLL = scroll, $
  172.         SUPPRESS_VALUE = sup, $
  173.         TITLE = title, $
  174.         UVALUE = uval, $
  175.         VALUE = val, $
  176.         VERTICAL = vert, $
  177.         XSIZE = xsize, $
  178.         YSIZE = ysize, $
  179.         FORMAT=format
  180.  
  181.   IF (N_PARAMS() EQ 0) THEN MESSAGE, 'Incorrect number of arguments'
  182.  
  183.   ON_ERROR, 2                        ;return to caller
  184.  
  185.   ; Defaults for keywords
  186.   IF NOT (KEYWORD_SET(drag))  THEN drag = 0
  187.   IF NOT (KEYWORD_SET(edit))  THEN edit = 0
  188.   IF NOT (KEYWORD_SET(frame)) THEN frame = 0
  189.   IF N_ELEMENTS(max) EQ 0     THEN max = 100.0
  190.   IF N_ELEMENTS(min) EQ 0     THEN min = 0.0
  191.   IF NOT (KEYWORD_SET(scroll)) THEN scroll = 10000 ELSE $
  192.   scroll = ABS(LONG((float(scroll) / (max - min)) * 1000000))
  193.   IF NOT (KEYWORD_SET(sup))   THEN sup = 0
  194.   IF NOT (KEYWORD_SET(title)) THEN title = ""
  195.   IF NOT (KEYWORD_SET(uval))  THEN uval = 0
  196.   IF N_ELEMENTS(val) EQ 0     THEN val = min
  197.   IF NOT KEYWORD_SET(format)  THEN format='(G13.6)'
  198.  
  199.   state = {slideid:0L, labelid:0L, top:max, bot:min, format:format }
  200.  
  201.   ; Motif 1.1 and newer sliders react differently to XSIZE and YSIZE
  202.   ; keywords than Motif 1.0 or OpenLook. These defs are for horizontal sliders
  203.   version = WIDGET_INFO(/version)
  204.   newer_motif = (version.style eq 'Motif') and (version.release ne '1.0')
  205.  
  206.   ; The sizes of the parts depend on keywords and whether or not the
  207.   ; float slider is vertical or horizontal
  208.   ;these are display specific and known to be inherently evil
  209.   sld_thk = 16
  210.   chr_wid = 7
  211.   IF (KEYWORD_SET(vert)) THEN BEGIN
  212.     if (newer_motif) then begin
  213.       if (not KEYWORD_SET(xsize)) then xsize = 0
  214.     endif else begin
  215.       title_len = STRLEN(title) * chr_wid
  216.       xsize = (sld_thk * 1.4) + title_len    ; Take label into account
  217.     endelse
  218.     IF NOT (KEYWORD_SET(ysize)) THEN ysize = 100
  219.     l_yoff = ysize / 2
  220.   ENDIF ELSE BEGIN                    ;horizontal slider
  221.     vert = 0
  222.     tmp = not keyword_set(xsize)
  223.     if (newer_motif) then begin
  224.       if (tmp) then xsize = 0
  225.       IF NOT (KEYWORD_SET(ysize)) THEN ysize = 0
  226.     endif else begin
  227.       if (tmp) then xsize = 100
  228.       IF (TITLE NE '') THEN sld_thk = sld_thk + 21
  229.       ysize = sld_thk        ; Make the slider not waste label space
  230.     endelse
  231.     l_yoff = 0
  232.   ENDELSE
  233.  
  234.   if (vert) then begin
  235.     mainbase = WIDGET_BASE(parent, FRAME = frame, /ROW)
  236.     labelbase = WIDGET_BASE(mainbase)
  237.   endif else begin
  238.     mainbase = WIDGET_BASE(parent, FRAME = frame, /COLUMN)
  239.     labelbase = mainbase
  240.   endelse
  241.   WIDGET_CONTROL, mainbase, SET_UVALUE = uval, EVENT_FUNC = 'fslide_event', $
  242.     PRO_SET_VALUE='FSLIDER_SET_VALUE', $
  243.     FUNC_GET_VALUE='FSLIDER_GET_VALUE'
  244.  
  245.  
  246.  
  247.   IF (sup EQ 0) THEN $
  248.     ; Only build the label if suppress_value is FALSE
  249.     state.labelid = WIDGET_TEXT(labelbase, YOFFSET = l_yoff, $
  250.         VALUE = STRING(FLOAT(val), format=state.format), $
  251.                 edit=edit) $
  252.   ELSE state.labelid = 0
  253.  
  254.     state.slideid = WIDGET_SLIDER(mainbase, $
  255.         TITLE = TITLE, $
  256.         XSIZE = xsize, $
  257.         YSIZE = ysize, $
  258.         /SUPPRESS_VALUE, $
  259.         MINIMUM = 0, $
  260.         MAXIMUM = 1000000, $
  261.         VALUE = 1000000. * $
  262.             (float(val) - state.bot) / $
  263.             (state.top - state.bot), $
  264.         VERTICAL = vert, $
  265.         DRAG=drag, $
  266.         SCROLL=scroll)
  267.  
  268.   
  269.   WIDGET_CONTROL, WIDGET_INFO(mainbase, /CHILD), SET_UVALUE=state, /NO_COPY
  270.   RETURN, mainbase
  271.  
  272. END
  273.